package com.heyu.cgsd.ui.activity

import android.Manifest
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.view.KeyEvent
import com.heyu.cgsd.R
import com.heyu.cgsd.base.BaseActivity
import com.heyu.cgsd.config.CgsdConfig
import com.heyu.cgsd.ui.dialog.DialogPolicy
import com.qw.soul.permission.SoulPermission
import com.qw.soul.permission.bean.Permission
import com.qw.soul.permission.bean.Permissions
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener

class SplashActivity : BaseActivity() {

    private var isStart = false

    private var permission = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE)


    private var dialogPolicy: DialogPolicy? = null
    private fun showDialog() {
        if (dialogPolicy != null) {
            dialogPolicy!!.dismiss()
            dialogPolicy = null
        }
        dialogPolicy = DialogPolicy(this, object : DialogPolicy.OnPolicyListener {
            override fun onPolicy() {
                UserServiceAgreementActivity.startAct(this@SplashActivity, 1)
            }

            override fun onUser() {
                UserServiceAgreementActivity.startAct(this@SplashActivity, 0)
            }

            override fun onCancel() {
                dialogPolicy!!.dismiss()
                CgsdConfig.setAgreed(false)
                finish()
            }

            override fun onAgreed() {
                dialogPolicy!!.dismiss()
                CgsdConfig.setAgreed(true)
                checkAndRequestPermissions()
            }
        })
        dialogPolicy!!.show()
    }

    override fun initView() {
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }

        if (!CgsdConfig.getAgreed()) {
            showDialog()
        } else {
            checkAndRequestPermissions()
        }
    }

    private fun checkAndRequestPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            SoulPermission.getInstance().checkAndRequestPermissions(
                    Permissions.build(*permission), object : CheckRequestPermissionsListener {
                override fun onAllPermissionOk(allPermissions: Array<Permission>) {
                    startAct()
                }

                override fun onPermissionDenied(refusedPermissions: Array<Permission>) {
                    startAct()
                }
            })
        } else {
            startAct()
        }
    }

    override fun getContentView(): Int? {
        setTitleBackground(BG_BLACK)
        return R.layout.activity_splash
    }

    private fun startAct() {
        isStart = if (isStart) {
            Handler().postDelayed({
                startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                this@SplashActivity.finish()
                false
            }, 1000)
        } else {
            true
        }
    }

    override fun onResume() {
        super.onResume()
        if (isStart) {
            startAct()
        }
        isStart = true
    }

    override fun onPause() {
        super.onPause()
        isStart = false
    }

    /**
     * 开屏页一定要禁止用户对返回按钮的控制，否则将可能导致用户手动退出了App而广告无法正常曝光和计费
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (dialogPolicy != null) {
            dialogPolicy!!.dismiss()
            dialogPolicy = null
        }
    }
}